package com.tjp.eight.ui.fragment.tabhome;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tjp.eight.R;
import com.tjp.eight.utils.DipPxSpUtils;
import com.tjp.eight.utils.StringUtils;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/2/13.
 */

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<HomeListModel.HomeListItemModel> homeListModels;
    private Context context;
    private int type = 0;
    private HomeItemOnClickListener homeItemOnClickListener;
    private static final int TYPE_ONE = 1;
    private static final int TYPE_TWO = 2;
    private static final int TYPE_THREE = 3;

    public HomeAdapter(Context context) {
        this.context = context;
        homeListModels = new ArrayList<>();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_ONE:
                return new HomeViewHolderLarge(LayoutInflater.from(context).inflate(R.layout.item_main_large, parent, false));
            case TYPE_TWO:
                return new HomeViewHolderSmall(LayoutInflater.from(context).inflate(R.layout.item_main_small, parent, false));
            case TYPE_THREE:
                return new HomeViewHolderHu(LayoutInflater.from(context).inflate(R.layout.item_main_interact, parent, false));
            case 4:
                return new HomeViewHolderHuLarge(LayoutInflater.from(context).inflate(R.layout.item_main_interact_large, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_ONE:
                ((HomeViewHolderLarge) holder).tv_show_read.setText("" + StringUtils.toWan(homeListModels.get(position).views_all));
                ((HomeViewHolderLarge) holder).tv_show_title.setText(homeListModels.get(position).title);
                ((HomeViewHolderLarge) holder).tv_show_tips.setText(homeListModels.get(position).caid_name);
                Glide.with(context).load(homeListModels.get(position).cover_path).error(context.getResources().getDrawable(R.drawable.loading)).into(((HomeViewHolderLarge) holder).iv_pic);
                break;
            case TYPE_TWO:
                ((HomeViewHolderSmall) holder).tv_show_read.setText("" + StringUtils.toWan(homeListModels.get(position).views_all));
                ((HomeViewHolderSmall) holder).tv_show_title.setText(homeListModels.get(position).title);
                ((HomeViewHolderSmall) holder).tv_show_tips.setText(homeListModels.get(position).caid_name);
                Glide.with(context).load(homeListModels.get(position).cover_path).error(context.getResources().getDrawable(R.drawable.loading)).into(((HomeViewHolderSmall) holder).iv_pic);
                break;
            case TYPE_THREE:
                ((HomeViewHolderHu) holder).tv_interact_title.setText("" + homeListModels.get(position).title);
                ((HomeViewHolderHu) holder).tv_interact_title_views.setText("" + getS(homeListModels.get(position).views_all) + "人已参与");
                Glide.with(context).load(homeListModels.get(position).cover_path).error(context.getResources().getDrawable(R.drawable.loading)).into(((HomeViewHolderHu) holder).iv_interact);
                break;
            case 4:
                ((HomeViewHolderHuLarge) holder).tv_interact_title.setText("" + homeListModels.get(position).title);
                ((HomeViewHolderHuLarge) holder).tv_interact_title_views.setText("" + getS(homeListModels.get(position).views_all) + "人已参与");
                Glide.with(context).load(homeListModels.get(position).cover_path).error(context.getResources().getDrawable(R.drawable.loading)).into(((HomeViewHolderHuLarge) holder).iv_interact);
                break;
        }
    }


    @Override
    public int getItemCount() {
        return homeListModels == null ? 0 : homeListModels.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (type == 1) {
            if (position == 0) {
                return 4;
            }
            return TYPE_THREE;
        }
        if (position % 5 == 0) {
            return TYPE_ONE;
        }
        return TYPE_TWO;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setHomeItemOnClickListener(HomeItemOnClickListener homeItemOnClickListener) {
        this.homeItemOnClickListener = homeItemOnClickListener;
    }

    private String getS(int views_all) {
        if (views_all >= 10000 && views_all < 999999) {
            int vi = views_all / 10000;
            return vi + "万+";
        } else if (views_all < 10000 && views_all >= 1000) {
            int vi = views_all / 1000;
            return vi + "千+";
        } else if (views_all < 1000 && views_all >= 100) {
            int vi = views_all / 100;
            return vi + "百+";
        }
        return views_all + "";
    }

    public void addData(HomeListModel homeListModel) {
        if (homeListModel == null || homeListModel.data == null || homeListModel.data.isEmpty())
            return;
        homeListModels.clear();
        for (HomeListModel.HomeListItemModel homeListItemModel : homeListModel.data) {
            homeListModels.add(homeListItemModel);
        }

        notifyDataSetChanged();
    }

    public void setType(int type) {
        this.type = type;
    }

    public void addData(ArrayList<HomeListModel.HomeListItemModel> homeListModels) {
        if (homeListModels == null || homeListModels.isEmpty()) return;
        for (HomeListModel.HomeListItemModel homeListModel : homeListModels)
            this.homeListModels.add(homeListModel);
        notifyDataSetChanged();
    }

    public interface HomeItemOnClickListener {
        void onClick(HomeListModel.HomeListItemModel homeModel, int position);
    }

    private class HomeViewHolderLarge extends RecyclerView.ViewHolder implements View.OnClickListener {
        View view;
        ImageView iv_pic;
        TextView tv_show_title;
        TextView tv_show_tips;
        TextView tv_show_read;

        public HomeViewHolderLarge(View view) {
            super(view);
            this.view = view;
            iv_pic = (ImageView) view.findViewById(R.id.iv_pic);
            tv_show_title = (TextView) view.findViewById(R.id.tv_show_title);
            tv_show_tips = (TextView) view.findViewById(R.id.tv_show_tips);
            tv_show_read = (TextView) view.findViewById(R.id.tv_show_read);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (homeItemOnClickListener != null) {
                homeItemOnClickListener.onClick(homeListModels.get(getLayoutPosition()), getLayoutPosition());
            }
        }
    }

    private class HomeViewHolderSmall extends RecyclerView.ViewHolder implements View.OnClickListener {
        View view;
        ImageView iv_pic;
        TextView tv_show_title;
        TextView tv_show_tips;
        TextView tv_show_read;

        public HomeViewHolderSmall(View view) {
            super(view);
            this.view = view;
            iv_pic = (ImageView) view.findViewById(R.id.iv_pic);
            tv_show_title = (TextView) view.findViewById(R.id.tv_show_title);
            tv_show_tips = (TextView) view.findViewById(R.id.tv_show_tips);
            tv_show_read = (TextView) view.findViewById(R.id.tv_show_read);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (homeItemOnClickListener != null) {
                homeItemOnClickListener.onClick(homeListModels.get(getLayoutPosition()), getLayoutPosition());
            }
        }
    }

    private class HomeViewHolderHu extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView iv_interact;
        TextView tv_interact_title;
        TextView tv_interact_title_views;
        View view;

        public HomeViewHolderHu(View view) {
            super(view);
            this.view = view;
            iv_interact = (ImageView) view.findViewById(R.id.iv_interact);
            tv_interact_title = (TextView) view.findViewById(R.id.tv_interact_title);
            tv_interact_title_views = (TextView) view.findViewById(R.id.tv_interact_title_views);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (homeItemOnClickListener != null) {
                homeItemOnClickListener.onClick(homeListModels.get(getLayoutPosition()), getLayoutPosition());
            }
        }
    }

    private class HomeViewHolderHuLarge extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView iv_interact;
        TextView tv_interact_title;
        TextView tv_interact_title_views;

        View view;

        public HomeViewHolderHuLarge(View view) {
            super(view);

            this.view = view;
            iv_interact = (ImageView) view.findViewById(R.id.iv_interact);
            tv_interact_title = (TextView) view.findViewById(R.id.tv_interact_title);
            tv_interact_title_views = (TextView) view.findViewById(R.id.tv_interact_title_views);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (homeItemOnClickListener != null) {
                homeItemOnClickListener.onClick(homeListModels.get(getLayoutPosition()), getLayoutPosition());
            }
        }
    }
}
